using System;
using System.Collections;

namespace PHSRAG.Utility
{

	/// <summary>
	/// SortedHashList is derived from ArrayList and combines functionality
	/// of an ArrayList and Hashtable.  SortedHashList stores objects in its
	/// underying ArrayList.  The SortedHashList also has a Hashtable member 
	/// that has the following Key/Value pair information:
	///		Key - a unique identifier for an object stored in the underlying ArrayList
	///		Value - the index of the object in the underlying ArrayList
	///	There is a 1 to 1 relationship between objects stored in the underlying
	///	ArrayList and DictionaryItems stored in the Hashtable member.
	/// </summary>
	public class SortedHashList : ArrayList
	{
		#region Private Members
		Hashtable hashtable;
		#endregion
		
		#region Constructor
		/// <summary>
		/// Allocates memory for the private Hashtable member.
		/// </summary>
		public SortedHashList()
		{
			hashtable = new Hashtable();
		}
		#endregion

		/// <summary>
		/// Adds an item to the SortedHashList's underlying ArrayList.  Also adds an entry
		/// in the SortedHashList's HashTable with the value being the item's position in 
		/// the underlying ArrayList and the key being the unique identifier for the item.
		/// </summary>
		/// <param name="key">A unique identifier for the SortedHashList item entry.</param>
		/// <param name="value">The item to be stored in the SortedHashList.</param>
		/// <returns>Returns the index position in the array where the item was added or -1
		/// if the item already exists.</returns>
		public int Add( int key, object value )
		{
			int index = -1;
			if (!hashtable.Contains(key))
			{
				base.Add(value);
				hashtable.Add(key, Count - 1);
				index = Count - 1;
			}
			return index;
		}

		/// <summary>
		/// Gets an item object out of the array list based on the 
		/// unique key identifier passed in.
		/// </summary>
		/// <param name="key">The unique identifier for the item.</param>
		/// <returns>The item object in the underlying ArrayList or null if 
		/// the key does not exist.</returns>
		public object GetItem(int key)
		{
			object o = null;
			if ( hashtable.Contains(key) )
			{
				int index = (int)hashtable[key];
				o = base[index];
			}
		    return o;
		}
	}
}
